import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  childActivityList: {},
  currentChildActivity: {},
  searchChildActivityCondition: {
    pageSize: 5,
    childHomeId: "",
    title: ""
  }
};

const getters = {
  childActivityList: state => state.childActivityList,
  currentChildActivity: state => state.currentChildActivity,
  searchChildActivityCondition: state => state.searchChildActivityCondition
};

const actions = {
  getChildActivityPageS({ commit }, param) {
    return Api.getChildActivityPage(param).then(ret => {
      commit(types.UPDATE_CHILDACTIVITY_LIST, ret.data.data);
      return ret;
    });
  },
  getChildActivityS({ commit }, id) {
    return Api.getChildActivity({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_CHILDACTIVITY, ret.data.data);
      return ret;
    });
  },
  addOrUpdateChildActivityS({ commit }, payload) {
    return Api.addOrUpdateChildActivity({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteChildActivityS({ commit }, param) {
    return Api.deleteChildActivity(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_CHILDACTIVITY](state, payload) {
    state.currentChildActivity = {
      content: "",
      image: "",
      summary: "",
      title: "",
      visible: true,
      childHomeName: payload.childHomeName,
      childHomeId: payload.childHomeId
    };
  },
  [types.UPDATE_CURRENT_CHILDACTIVITY](state, payload) {
    state.currentChildActivity = payload;
  },
  [types.UPDATE_CHILDACTIVITY_LIST](state, payload) {
    state.childActivityList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
